import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import util from '../utils/util.js'

// 所有的路由数组
const routerList = [];

// 该函数用于将所有分区路由中的路由添加到路由数组
function importAll(routerArr) {
	routerArr.keys().forEach(key => {
		routerArr(key).default.forEach(item => {
			item.meta=item.meta || {};
			// item.meta.keepAlive=typeof(item.meta.keepAlive)==="undefined"?true:item.meta.keepAlive;
			item.meta.keepAlive=item.meta.keepAlive || false;
			routerList.push(item)
		});
		//routerList = routerList.concat(routerArr(key).default);
		//routerList.push(routerArr(key).default)
	})
}
importAll(require.context(".", true, /\.routes\.js/))

const routes = [
	...routerList
]

const router = new VueRouter({
	mode: 'history',
	routes: routes
})

router.beforeEach((to, from, next) => {
	if (to.matched.length === 0) {
		console.log(to);
	   next('/404') // 判断此跳转路由的来源路由是否存在，存在的情况跳转到来源路由，否则跳转到404页面
	   return;
	}
	if (to.meta.title) {
		document.title = to.meta.title
	}
	next();
});

router.afterEach(() => {

});

export default router
